package com.vrms.app.controller;

import com.vrms.app.aop.SystemControllerLog;
import com.vrms.app.entity.Administrators;
import com.vrms.app.service.AdministratorsService;
import com.vrms.app.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController   //语义化标签，表明该类是个控制器类
//可以处理用户的请求和相应的，功能类似于servlet
@RequestMapping("/administrators")  //配置请求时后台处理的路径
@Api(tags = "管理员控制器模块")
@CrossOrigin//解决跨域访问问题
public class AdministratorsController {
    @Resource
    private AdministratorsService administratorsService;
    //控制器注解
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始数据",example = "1",
                    required = true,dataType = "integer",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "size",value = "每页最大记录数",example = "10",
                    required = true,dataType = "integer",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "username",value = "根据管理员姓名查询",example = "张三",
                    required = true,dataType = "string",dataTypeClass = Long.class),
    }

    )
    @GetMapping("/listPage")
    @ApiOperation("分页查询管理员信息")
    @SystemControllerLog(description = "分页查询管理员信息")

    public CommonResult listPage(int page, int size, String username){
        Map<String,Object> map=new HashMap();
        map.put("count",administratorsService.count(username));
        map.put("data",administratorsService.listPage(page,size,username));
        return CommonResult.success(map);
    }

    @PostMapping("/save")
    @ApiOperation("添加管理员信息")
    @SystemControllerLog(description = "添加管理员信息")

    public CommonResult save(@RequestBody Administrators administrators){
        return CommonResult.success(administratorsService.save(administrators));
    }

    @PostMapping("/update")
    @ApiOperation("修改管理员信息")
    @SystemControllerLog(description = "修改管理员信息")

    public CommonResult update(@RequestBody Administrators administrators){
        return CommonResult.success(administratorsService.update(administrators));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid",value = "根据ID删除管理员信息",
            example = "1",dataType = "integer",required = true,dataTypeClass = Long.class)
    })
    @PostMapping("/remove")
    @ApiOperation("删除管理员信息")
    @SystemControllerLog(description = "删除管理员信息")

    public CommonResult remove(Integer aid){
        return CommonResult.success(administratorsService.remove(aid));
    }

    @PostMapping("/changeStats")
    @ApiOperation("改变用户状态")
    @SystemControllerLog(description = "改变用户状态")

    public CommonResult changeStats(@RequestBody Administrators administrators){
        return CommonResult.success(administratorsService.changeStats(administrators));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名称",
                    example = "小明", dataType = "string", required = true),
            @ApiImplicitParam(name = "password", value = "用户密码",
                    example = "888", dataType = "string", required = true)
    })
    @ApiOperation("管理员登录方法")
    @SystemControllerLog(description = "登录")
    @GetMapping("/login")
    public CommonResult login(String username, String password) {
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isAuthenticated()) {
                subject.login(token);//该方法是shiro提供的登录方法，该方法会自动跳转到用户定义的Realm中
            }
        } catch (AuthenticationException uae) {
            System.out.println("用户名称或者密码不正确");
            return CommonResult.failed();
        }
        return CommonResult.success(username);//登录成功返回username
    }

    @ApiOperation("用户注销")
    @GetMapping("/logout")
    public CommonResult logout() {
        SecurityUtils.getSubject().logout();
        return CommonResult.success(200);
    }
}
